package cn.itcast.erp.entity.utils;


import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * 发送邮件
 * @author 周昌洋
 * @date 2019/6/11
 */
@Component
public class SendMail  {

//    邮箱服务器的地址
    private  String SMTPSERVER = null;
//    发送邮件的端口
    private  String SMTPPORT = "465";
//    发送邮件的邮箱
    private  String ACCOUT = null;
//    接收数据的邮箱
    private  String sName = null;
//    授权码
    private  String PWD = null;
//    邮件的名字
    private  String accept = null;



    //设置发送邮箱(sendaddress),邮箱密码(password),发件邮箱昵称(可不写)(sendName),接受邮箱地址(acceptName)
    public void sendMail(String sendaddress,String password,String sendName, String acceptName){

        this.ACCOUT = sendaddress;
        this.PWD = password;
        this.sName = sendName;
        this.accept = acceptName;

    }

    //设置邮箱类型,比如163,qq...
    public void mailClass(String mailType){

        this.SMTPSERVER ="smtp."+ mailType+".com";
        //System.out.println(this.SMTPSERVER);
    }

    //发送邮件内容为(storealerts)
    public String mailTest(String storealerts){

        try {
            testSendEmail(storealerts);
            return "发送邮件成功";
        } catch (Exception e) {

            e.printStackTrace();
            return "发送邮件失败,请重新发送";
        }


    }


    public void testSendEmail(String storealerts) throws Exception {

        // 创建邮件配置
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", SMTPSERVER); // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.port", SMTPPORT);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
        props.setProperty("mail.smtp.ssl.enable", "true");// 开启ssl


        // 根据邮件配置创建会话，注意session别导错包
        Session session = Session.getDefaultInstance(props);
        // 开启debug模式，可以看到更多详细的输入日志
        session.setDebug(true);
        //创建邮件
        MimeMessage message = createEmail(session,storealerts);
        //获取传输通道
        Transport transport = session.getTransport();

        transport.connect(SMTPSERVER,ACCOUT, PWD);
        //连接，并发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }


    public MimeMessage createEmail(Session session,String storealerts) throws Exception {
        // 根据会话创建邮件
        MimeMessage msg = new MimeMessage(session);
        // address邮件地址, personal邮件昵称, charset编码方式
        InternetAddress fromAddress = new InternetAddress(ACCOUT,
                sName, "utf-8");
        // 设置发送邮件方
        msg.setFrom(fromAddress);
        InternetAddress receiveAddress = new InternetAddress(
                accept, "***", "utf-8");
        // 设置邮件接收方
        msg.setRecipient(Message.RecipientType.TO, receiveAddress);
        // 设置邮件标题
        msg.setSubject("库存预警邮件", "utf-8");
        msg.setText(storealerts);
        // 设置显示的发件时间
        msg.setSentDate(new Date());
        // 保存设置
        msg.saveChanges();
        return msg;
    }



}
